package com.hp.ssmp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.ssmp.entity.TAddress;
import com.hp.ssmp.entity.TUser;
import com.hp.ssmp.service.AddressService;
import com.hp.ssmp.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    //分页条件查询地址
    @GetMapping("/getAddressPage")
    public R<Page<TAddress>> list(Integer currentPage, Integer pageSize, Long userid) {
        Page<TAddress> list = addressService.getAddressPage(currentPage, pageSize, userid);
        return R.success(list);
    }

    //不分页查询地址
    @GetMapping("/getAddressList")
    public R<List<TAddress>> list(Long userid) {
        List<TAddress> list=addressService.getAddressList(userid);
        return R.success(list);
    }

    @PostMapping("/add")
    public R add(TAddress address) {
        boolean result = addressService.save(address);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @GetMapping("/del")
    public R del(Long addressid) {
        boolean result = addressService.removeById(addressid);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @PostMapping("/update")
    public R update(TAddress address) {
        boolean result = addressService.updateById(address);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    //    设为默认
    @GetMapping("/updDefault")
    public R updDefault(Long addressid, Long userid) {
        boolean result = addressService.updDefault(addressid, userid);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }
}
